namespace Cik.Gapi4Net.Service
{
    using Core.AcsynRequest;
    using Core.Json;
    using Domain.Output;
    using Entlib.Container;
    using Entlib.Logging;

    internal abstract class ServiceBase : ISearchService
    {
        protected ILoggerFactory _loggerFactory;
        protected ILog _logger;
        protected IDownloader _downloader;

        protected ServiceBase()
            : this(IoC.GetInstance<ILoggerFactory>(), IoC.GetInstance<IDownloader>())
        {
        }

        internal ServiceBase(ILoggerFactory loggerFactory, IDownloader downloader)
        {
            _loggerFactory = loggerFactory;
            _downloader = downloader;
            _logger = _loggerFactory.CreateLogger();
        }

        // trict here for stuffing json data
        public string Result { get; set; }

        protected TResult GetResultData<TResult>(string searchUrl) where TResult : IContractBase
        {
            _downloader.Add(searchUrl);
            _downloader.DownloadAll(x =>
            {
                Result = x.Result;
            }).ExecuteAndWait();

            var webSearchResult = JsonHelper.DeserializeObject<TResult>(Result);

            return webSearchResult;
        }
    }
}